import React, { useImperativeHandle, useState } from 'react';
import { Form, message } from 'antd';
import request from '@/utils/request';
import ScoreForm from './ScoreForm';
import EstimateForm from './EstimateForm';

const titleStyle = {
    fontSize: 16,
    color: '#000',
    padding: '12px 0 12px'
};
const ExtendRule = React.forwardRef((props, ref) => {
    const { form, tab = '' } = props;
    const [data, setData] = useState({});
    const restProps = {
        titleStyle
    };
    useImperativeHandle(ref, () => ({
        handleExpandSave: () => {
            form.validateFields(async (err, values) => {
                if (!err) {
                    // 因为后端传参需要外部多加一层字段区分，这里加一次数据过滤
                    const scroeArray = [
                        'reScoreLimitFlag',
                        'reScoreOverdueDays',
                        'scoreConfiguration',
                        'scoreLimitFlag',
                        'scoreOverdueDays',
                        'scoreOverdueType',
                        'scoreOverdueTypeValue',
                        'scoreUrgeHours',
                        'scoreUrgeIntervalHours',
                        'scoreUrgeLimitFlag',
                        'scoreUrgeMaxTimes'
                    ];
                    const estimateArray = [
                        'estimationConfiguration',
                        'estimationLimitFlag',
                        'estimationOverdueDays',
                        'estimationUrgeHours',
                        'estimationUrgeIntervalHours',
                        'estimationUrgeLimitFlag',
                        'estimationUrgeMaxTimes',
                        'reEstimationLimitFlag',
                        'reEstimationOverdueDays'
                    ];
                    const taskBusinessExtendScoreRuleDTO = {};
                    const taskBusinessExtendEstimationRuleDTO = {};

                    Object.keys(values).forEach(key => {
                        if (scroeArray.includes(key)) {
                            if (key === 'scoreConfiguration') {
                                taskBusinessExtendScoreRuleDTO[key] = values[key].filter(
                                    item => !String(item).includes('company')
                                );
                            } else {
                                taskBusinessExtendScoreRuleDTO[key] = values[key];
                            }
                        }
                    });
                    Object.keys(values).forEach(key => {
                        if (estimateArray.includes(key)) {
                            if (key === 'estimationConfiguration') {
                                taskBusinessExtendEstimationRuleDTO[key] = values[key].filter(
                                    item => !String(item).includes('company')
                                );
                            } else {
                                taskBusinessExtendEstimationRuleDTO[key] = values[key];
                            }
                        }
                    });

                    const payload = {
                        taskBusinessExtendScoreRuleDTO,
                        taskBusinessExtendEstimationRuleDTO
                    };
                    console.log('Received values of form: ', payload);

                    const result = await request('/api/oa/v1/work/task/business_extend_rule', {
                        method: 'PUT',
                        data: payload
                    });
                    if (result.code === 10000) {
                        message.success(result.msg);
                    } else {
                        message.error(result.msg);
                    }
                    // console.log('Received values of form: ', payload);
                }
            });
        }
    }));
    React.useEffect(() => {
        if (tab === 'tab2') {
            form.resetFields();
            request('/api/oa/v1/work/task/business_extend_rule', {
                method: 'GET'
            }).then(result => {
                if (result.code === 10000) {
                    setData(result.data);
                } else {
                    message.error(result.msg);
                }
            });
        }
    }, [tab]);
    return (
        <Form layout="inline" style={{ marginBottom: 80 }}>
            <ScoreForm form={form} {...restProps} data={data} />
            <EstimateForm form={form} {...restProps} data={data} />
        </Form>
    );
});
export default Form.create()(ExtendRule);
